package com.ironman.analyze.core.compute.entity;

import cn.hutool.core.util.StrUtil;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.annotation.write.style.ColumnWidth;
import com.ironman.common.util.AccuracyUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.Arrays;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;

@Setter
@Getter
public class TrainingData {

    @ExcelProperty("训练时间")
    @ColumnWidth(20)
    private Date ts;

    @ExcelProperty("经度")
    private String lon;
    @ExcelProperty("纬度")
    private String lat;

    @ExcelProperty("教练艇")
    @ColumnWidth(12)
    private String windSourceBoatCode;
    /**
     * 矢量风速
     */
    @ExcelProperty("风速")
    private Double windSpeed;
    /**
     * 矢量风向
     */
    @ExcelProperty("风向")
    private Double windDirection;
    @ExcelProperty("自定义风速")
    @ColumnWidth(18)
    private Double customWindSpeed;
    @ExcelProperty("自定义风向")
    @ColumnWidth(18)
    private Integer customWindDirection;
    /**
     * 算数船速
     */
    @ExcelProperty("船速")
    private Double speed;
    @ExcelProperty("标准船速")
    @ColumnWidth(18)
    private Double standardSpeed;
    @ExcelProperty("自定义标准船速")
    @ColumnWidth(24)
    private Double customStandardSpeed;

    @ExcelProperty("真北航向")
    @ColumnWidth(16)
    private Double headingT;
    @ExcelProperty("磁北航向")
    @ColumnWidth(16)
    private Double headingM;
    @ExcelProperty("漂角")
    private String leeway;
    @ExcelProperty("姿态")
    private String sailStatus;

    @ExcelProperty("船首向")
    private Double yaw;
    @ExcelProperty("平衡")
    private Double rol;
    @ExcelProperty("姿态")
    private String posture;

    @ExcelProperty("角度")
    private Double angle;
    @ExcelProperty("自定义角度")
    @ColumnWidth(18)
    private Double customAngle;
    @ExcelProperty("VMG")
    private Double vmg;
    @ExcelProperty("标准VMG")
    @ColumnWidth(18)
    private Double standardVmg;
    @ExcelProperty("自定义VMG")
    @ColumnWidth(18)
    private Double customVmg;
    @ExcelProperty("自定义标准VMG")
    @ColumnWidth(20)
    private Double customStandardVmg;
    @ExcelProperty("心率")
    private String heartrate;

    public void setHeartrate(Double[] heartrate) {
        this.heartrate = Arrays.stream(new Integer[]{AccuracyUtil.downHeartrateInteger(heartrate[0]), AccuracyUtil.downHeartrateInteger(heartrate[1])}).map(String::valueOf).collect(Collectors.joining("#"));
    }

    public String compress() {
        // ts,lon,lat,windSourceBoatCode,windSpeed,windDirection,customWindSpeed,customWindDirection,speed,standardSpeed,customStandardSpeed,headingT,headingM,leeway,sailStatus,yaw,rol,posture,angle,customAngle,vmg,standardVmg,customVmg,customStandardVmg,heartrate
        return StrUtil.format("{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
                ts.getTime() / 1000,
                StrUtil.isNotBlank(lon) ? AccuracyUtil.gpsStrToDouble(lon) : "",
                StrUtil.isNotBlank(lat) ? AccuracyUtil.gpsStrToDouble(lat) : "",
                StrUtil.isNotBlank(windSourceBoatCode) ? windSourceBoatCode : "",
                Objects.nonNull(windSpeed) ? AccuracyUtil.halfUpSpeedDouble(windSpeed) : "",
                Objects.nonNull(windDirection) ? AccuracyUtil.downDirectionInteger(windDirection) : "",
                Objects.nonNull(customWindSpeed) ? AccuracyUtil.halfUpSpeedDouble(customWindSpeed) : "",
                Objects.nonNull(customWindDirection) ? customWindDirection : "",
                Objects.nonNull(speed) ? AccuracyUtil.halfUpSpeedDouble(speed) : "",
                Objects.nonNull(standardSpeed) ? AccuracyUtil.halfUpSpeedDouble(standardSpeed) : "",
                Objects.nonNull(customStandardSpeed) ? AccuracyUtil.halfUpSpeedDouble(customStandardSpeed) : "",
                Objects.nonNull(headingT) ? AccuracyUtil.downDirectionInteger(headingT) : "",
                Objects.nonNull(headingM) ? AccuracyUtil.downDirectionInteger(headingM) : "",
                Objects.nonNull(leeway) ? leeway : "",
                Objects.nonNull(sailStatus) ? sailStatus : "",
                Objects.nonNull(yaw) ? AccuracyUtil.downDirectionInteger(yaw) : "",
                Objects.nonNull(rol) ? AccuracyUtil.downDirectionInteger(rol) : "",
                Objects.nonNull(posture) ? posture : "",
                Objects.nonNull(angle) ? AccuracyUtil.downDirectionInteger(angle) : "",
                Objects.nonNull(customAngle) ? AccuracyUtil.downDirectionInteger(customAngle) : "",
                Objects.nonNull(vmg) ? AccuracyUtil.halfUpSpeedDouble(vmg) : "",
                Objects.nonNull(standardVmg) ? AccuracyUtil.halfUpSpeedDouble(standardVmg) : "",
                Objects.nonNull(customVmg) ? AccuracyUtil.halfUpSpeedDouble(customVmg) : "",
                Objects.nonNull(customStandardVmg) ? AccuracyUtil.halfUpSpeedDouble(customStandardVmg) : "",
                heartrate
        );
    }
}
